package sncf.client;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import sncf.utils.Converter;

public class Train {

	// FIELDS
	private String key;

	private String type;
	private String number;
	private Date dateHeure;
	private String from;
	private String to;
	private String situation;
	private String track;

	private int minutesLate;

	private boolean minutesLateConfirmed;
	private boolean eligibleIndemnite;

	private boolean modified;

	// CONSTRUCTORS
	public Train() {
		this(null, null, null, null, null, null, null);
	}

	public Train(String type, String number, Date dateHeure, String from,
			String to, String situation, String track) {
		this.type = type;
		this.number = number;
		this.dateHeure = dateHeure;
		this.from = from;
		this.to = to;
		this.situation = situation;
		handleSituation(this.situation);
		this.track = track;

		this.modified = false;
	}

	// METHODS
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
		setModified(true);
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
		setModified(true);
	}

	public Date getDateHeure() {
		return dateHeure;
	}

	public void setDateHeure(Date dateHeure) {
		this.dateHeure = dateHeure;
		setModified(true);
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
		setModified(true);
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
		setModified(true);
	}

	public String getSituation() {
		return situation;
	}

	public void setSituation(String situation) {
		this.situation = situation;
		handleSituation(situation);
		setModified(true);
	}

	public String getTrack() {
		return track;
	}

	public void setTrack(String track) {
		this.track = track;
		setModified(true);
	}

	public int getMinutesLate() {
		return this.minutesLate;
	}

	public boolean isMinutesLateConfirmed() {
		return minutesLateConfirmed;
	}

	public void setConfirmedMinutesLates(int confirmedMinutesLate) {
		this.minutesLate = confirmedMinutesLate;
		setMinutesLateConfirmed(true);
	}

	private void setMinutesLateConfirmed(boolean minutesLateConfirmed) {
		this.minutesLateConfirmed = minutesLateConfirmed;
	}

	public boolean isEligibleIndemnite() {
		return eligibleIndemnite;
	}

	public void setEligibleIndemnite(boolean eligibleIndemnite) {
		this.eligibleIndemnite = eligibleIndemnite;
	}

	public boolean isModified() {
		return modified;
	}

	private void setModified(boolean modified) {
		this.modified = modified;
	}

	// TOOLS

	private void handleSituation(String situation) {
		if (situation == null) {
			this.minutesLate = 0;
		} else {
			situation = situation.replaceAll(" ", "");
			String[] split = situation.split(":");

			if (split != null && split.length == 2
					&& "retard".equalsIgnoreCase(split[0])) {
				this.minutesLate = Converter
						.convertStringDayToMinutes(split[1]);
			}
		}

		// UpdateFromDate();
	}

	public void setMinutesLate(int minutesLate) {
		this.minutesLate = minutesLate;
	}

	private void UpdateFromDate() {
		if (getDateHeure() != null) {
			int todayMinutesConverted = Converter
					.convertTimeToDayMinutes(new Date());
			int destinationDateMinutesConverted = Converter
					.convertTimeToDayMinutes(getDateHeure());

			/*
			 * Ex: today = 0h30, destination date = 23h50, minutes late = 0mn
			 * Ex: today = 7h30, destination date = 10h00, minutes late = 0mn
			 */
			if (todayMinutesConverted < destinationDateMinutesConverted
					+ getMinutesLate()) {
				// From date is in the previous day

				Calendar cal = Calendar.getInstance();
				cal.setTime(getDateHeure());
				cal.add(Calendar.DAY_OF_MONTH, -1);

				setDateHeure(cal.getTime());
			}
		}
	}

	private String getDateHeureString(Date dateHeure) {
		SimpleDateFormat dateFormater = new SimpleDateFormat(
				"E dd/MM/yyyy HH:mm");

		return dateFormater.format(dateHeure);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime
				* result
				+ ((dateHeure == null) ? 0 : getDateHeureString(dateHeure)
						.hashCode());
		result = prime * result + ((number == null) ? 0 : number.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Train other = (Train) obj;
		if (dateHeure == null) {
			if (other.dateHeure != null)
				return false;
		} else if (!getDateHeureString(dateHeure).equals(
				other.dateHeure == null ? null
						: getDateHeureString(other.dateHeure)))
			return false;
		if (number == null) {
			if (other.number != null)
				return false;
		} else if (!number.equals(other.number))
			return false;
		return true;
	}

	@Override
	public String toString() {
		SimpleDateFormat dateFormater = new SimpleDateFormat(
				"E dd/MM/yyyy HH:mm");
		String formattedDate = dateHeure == null ? null : dateFormater
				.format(dateHeure);
		return "Train [type=" + type + ", number=" + number + ", dateHeure="
				+ formattedDate + ", from=" + from + ", to=" + to
				+ ", situation=" + situation + ", track=" + track
				+ ", minutesLate=" + minutesLate + "]";
	}

}
